package lg.minecraft.plugin.pokemonmenu.handler.input;

import lg.minecraft.plugin.pokemonmenu.command.InputType;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author langle__
 * @version 1.0
 */
public class PlayerInputManager {

    private static final Map<UUID, InputType> pendingInputTypes = new HashMap<>();
    private static final Map<UUID, PlayerInputHandler> pendingInputs = new HashMap<>();

    public static void waitForInput(Player player, PlayerInputHandler handler) {
        waitForInput(player, InputType.CUSTOM, handler);
    }

    public static void waitForInput(Player player,InputType type, PlayerInputHandler handler) {
        pendingInputTypes.put(player.getUniqueId(), type);
        pendingInputs.put(player.getUniqueId(), handler);
    }

    public static boolean isWaitingForInput(Player player) {
        return pendingInputs.containsKey(player.getUniqueId());
    }

    /**
     * 判断指定玩家是否正在等待某个特定类型的输入
     *
     * @param player 玩家对象
     * @param type   要检查的输入类型
     * @return 是否匹配
     */
    public static boolean isWaitingForInputOfType(Player player, InputType type) {
        InputType currentType = getPendingInputType(player);
        return currentType != null && currentType == type;
    }

    /**
     * 获取指定玩家当前等待的输入类型
     *
     * @param player 玩家对象
     * @return 输入类型，若无则返回 null
     */
    public static InputType getPendingInputType(Player player) {
        return pendingInputTypes.get(player.getUniqueId());
    }

    public static void handleInput(Player player, String input) {
        UUID uuid = player.getUniqueId();
        PlayerInputHandler handler = pendingInputs.remove(uuid);
        if (handler != null) {
            handler.onPlayerInput(player, input);
//            Bukkit.getScheduler().runTask(LGPokemonMenu.getInstance(), () -> handler.onPlayerInput(player, input));
        }
        pendingInputTypes.remove(uuid);
    }

}
